from PySide2.QtWidgets import QApplication, QWidget, QHBoxLayout
import sys
from PySide2.QtCharts import QtCharts
from PySide2.QtGui import QPainter, QPen, QColor
from PySide2.QtCore import Qt
import openpyxl

class ShowPage(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(1000, 700)
        self.chart = QtCharts.QChart()  # 图
        self.chartview = QtCharts.QChartView(self.chart) # 图层
        self.series = QtCharts.QHorizontalBarSeries()  #数据
        self.axis = QtCharts.QBarCategoryAxis()
        self.wb = openpyxl.load_workbook('C:\\Users\\zuozh\\Desktop\\太原科大Python综合培训\\国家数据\\就业人员和工资\\按经济类型分乡村就业人员.xlsx')
        self.ws = self.wb['年度数据']
        self.buildChart()

    def buildChart(self):
        self.series.setName('2018年人口年龄结构')
        for j in range(4, 7):
            barset = QtCharts.QBarSet(str(self.ws.cell(row=j, column=1).value))
            for i in range(2, 10):
                barset.append(
                    int(self.ws.cell(row=j, column=i).value)
                )
            self.series.append(barset)
        self.series.setLabelsVisible(True)

        nameList = list()
        for i in range(2, 10):
            nameList.append(str(self.ws.cell(row=3, column=i).value))
        self.axis.append(
            nameList
        )

        self.chart.setTitle('条图示例')
        self.chart.addAxis(self.axis, Qt.AlignLeft)
        self.chart.setAxisY(self.axis, self.series)

        self.chart.setAnimationOptions(QtCharts.QChart.SeriesAnimations)    # 动画
        # self.chart.legend().hide()  # 隐藏图例
        self.chart.addSeries(self.series)   # 输入数据
        layout = QHBoxLayout()
        layout.addWidget(self.chartview)
        self.setLayout(layout)
        self.chartview.setRenderHint(QPainter.Antialiasing) # 防止变形

if __name__ == "__main__":
    app = QApplication(sys.argv)
    v = ShowPage()
    v.show()
    sys.exit(app.exec_())
